package com.nannybear.Controller;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.nannybear.R;

import java.util.Observable;
import java.util.Observer;

/**
 * @brief Manage the thresholds received and change the view.
 */
public class CriticalityThresholds implements Observer{
    /********** DECLARATIONS **********/
    // Variables
    private int valueThresholdTemp;
    private int valueThresholdHum;
    private int NORMAL = 1;
    private int MODERATE = 2;
    private int CRITICAL = 3;
    private boolean timerTempActivated;
    private boolean timerHumActivated;

    // Class
    ViewPuppeteer viewPuppeteer;

    // Handler
    final Handler myHandler = new Handler();
    final Handler myTimer = new Handler();

    /************ METHODS ************/
    /**
     * @brief constructor.
     */
    public CriticalityThresholds(ViewPuppeteer viewPuppeteer){
        this.viewPuppeteer = viewPuppeteer;
        timerTempActivated = false;
        timerHumActivated = false;
    }

    /**
     * @brief Change the temperature tab when the threshold is normal
     */
    public void changeTabTempNormal(){
        this.getViewPuppeteer().getmActivity().getBabyStateScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey);
        this.getViewPuppeteer().getmActivity().getBrightnessScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey);
        this.getViewPuppeteer().getmActivity().getHelpScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey);
        this.getViewPuppeteer().getmActivity().getHomeScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey);
        this.getViewPuppeteer().getmActivity().getHumidityScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey);
        this.getViewPuppeteer().getmActivity().getSettingsScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey);
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getTempScreen()){
            this.getViewPuppeteer().getmActivity().getTempScreen().getTempButton().setBackgroundResource(R.drawable.temp_active);
        }
        else{
            this.getViewPuppeteer().getmActivity().getTempScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey);
        }
    }

    /**
     * @brief Change the temperature tab when the threshold is moderate
     */
    public void changeTabTempModerate(){
        this.getViewPuppeteer().getmActivity().getTempScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
        this.getViewPuppeteer().getmActivity().getBrightnessScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
        this.getViewPuppeteer().getmActivity().getHelpScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
        this.getViewPuppeteer().getmActivity().getHomeScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
        this.getViewPuppeteer().getmActivity().getHumidityScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
        this.getViewPuppeteer().getmActivity().getSettingsScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getTempScreen()){
            this.getViewPuppeteer().getmActivity().getTempScreen().getTempButton().setBackgroundResource(R.drawable.temp_active_moderate);
        }
        else{
            this.getViewPuppeteer().getmActivity().getTempScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
        }
    }

    /**
     * @brief Change the temperature tab when the threshold is critical
     */
    public void changeTabTempCritical(){
        Log.e("Criticality","ici");
        this.getViewPuppeteer().getmActivity().getTempScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
        this.getViewPuppeteer().getmActivity().getBrightnessScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
        this.getViewPuppeteer().getmActivity().getHelpScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
        this.getViewPuppeteer().getmActivity().getHomeScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
        this.getViewPuppeteer().getmActivity().getHumidityScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
        this.getViewPuppeteer().getmActivity().getSettingsScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getTempScreen()){
            this.getViewPuppeteer().getmActivity().getTempScreen().getTempButton().setBackgroundResource(R.drawable.temp_active_critical);
        }
        else{
            this.getViewPuppeteer().getmActivity().getTempScreen().getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
        }
    }

    /**
     * @brief Display a warning for the temperature
     */
    public void displayWarningTemp(){
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getTempScreen()){
            this.getViewPuppeteer().getmActivity().getTempScreen().getWarning().setVisibility(View.VISIBLE);
        }
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getHomeScreen()){
            this.getViewPuppeteer().getmActivity().getHomeScreen().getWarning_temp().setVisibility(View.VISIBLE);
        }
    }

    /**
     * @brief Display a warning for the humidity
     */
    public void deleteWarningTemp(){
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getTempScreen()){
            this.getViewPuppeteer().getmActivity().getTempScreen().getWarning().setVisibility(View.GONE);
        }
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getHomeScreen()){
            this.getViewPuppeteer().getmActivity().getHomeScreen().getWarning_temp().setVisibility(View.GONE);
        }
    }

    /**
     * @brief Display a pop up for the thresholds
     */
    public void displayPopUp(){
        final AlertDialog popUp = new AlertDialog.Builder(getViewPuppeteer().getmActivity().getWindow().getContext()).create();
        popUp.setTitle("BE CAREFUL");
        if(isTimerTempActivated() && !isTimerHumActivated()){
            popUp.setMessage("Temperature is in a critical state.\nIf you want more information go on the temperature tab.");
        }
        if(!isTimerTempActivated() && isTimerHumActivated()){
            popUp.setMessage("Humidity is in a critical state.\nIf you want more information go on the humidity tab.");
        }
        if(isTimerTempActivated() && isTimerHumActivated()){
            popUp.setMessage("Humidity and temperature are in a critical state.\nIf you want more information go on the appropriate tabs.");
        }
        popUp.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                popUp.cancel();
                startTimerTemp();
            }
        });
        popUp.setIcon(0);
        popUp.show();
    }

    /**
     * @brief Start the timer of temperature
     */
    private void startTimerTemp(){
        Log.e("thread","start");
        final Runnable tempTimer = new Runnable() {
            @Override
            public void run() {
                displayPopUp();
                Log.e("thread","stop");
            }
        };
        myTimer.postDelayed(tempTimer, 10000);
    }

    /**
     * @brief Change the temperature tab
     */
    public void changeConcerningTemp(){
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                int thresholdTemp;
                thresholdTemp = getValueThresholdTemp();
                if (thresholdTemp == NORMAL) {
                    setTimerTempActivated(false);
                    changeTabTempNormal();
                    deleteWarningTemp();
                    myTimer.removeCallbacksAndMessages(null);
                }
                if (thresholdTemp == MODERATE) {
                    setTimerTempActivated(false);
                    changeTabTempModerate();
                    displayWarningTemp();
                    myTimer.removeCallbacksAndMessages(null);
                }
                if (thresholdTemp == CRITICAL) {
                    setTimerTempActivated(true);
                    changeTabTempCritical();
                    displayWarningTemp();
                    displayPopUp();
                }
            }
        });
    }

    /**
     * @brief Change the humidity tab when the threshold is normal
     */
    public void changeTabHumNormal(){
        this.getViewPuppeteer().getmActivity().getBabyStateScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        this.getViewPuppeteer().getmActivity().getBrightnessScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        this.getViewPuppeteer().getmActivity().getHelpScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        this.getViewPuppeteer().getmActivity().getHomeScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        this.getViewPuppeteer().getmActivity().getSettingsScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        this.getViewPuppeteer().getmActivity().getTempScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        this.getViewPuppeteer().getmActivity().getTempScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getHumidityScreen()) {
            this.getViewPuppeteer().getmActivity().getHumidityScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_active);
        }
        else {
            this.getViewPuppeteer().getmActivity().getHumidityScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        }
    }

    /**
     * @brief Change the humidity tab when the threshold is moderate
     */
    public void changeTabHumModerate(){
        this.getViewPuppeteer().getmActivity().getTempScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
        this.getViewPuppeteer().getmActivity().getBrightnessScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
        this.getViewPuppeteer().getmActivity().getHelpScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
        this.getViewPuppeteer().getmActivity().getHomeScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
        this.getViewPuppeteer().getmActivity().getTempScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
        this.getViewPuppeteer().getmActivity().getSettingsScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getHumidityScreen()){
            this.getViewPuppeteer().getmActivity().getHumidityScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_active_moderate);
        }
        else{
            this.getViewPuppeteer().getmActivity().getHumidityScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
        }
    }

    /**
     * @brief Change the humidity tab when the threshold is critical
     */
    public void changeTabHumCritical(){
        this.getViewPuppeteer().getmActivity().getTempScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
        this.getViewPuppeteer().getmActivity().getBrightnessScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
        this.getViewPuppeteer().getmActivity().getHelpScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
        this.getViewPuppeteer().getmActivity().getHomeScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
        this.getViewPuppeteer().getmActivity().getTempScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
        this.getViewPuppeteer().getmActivity().getSettingsScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getHumidityScreen()){
            this.getViewPuppeteer().getmActivity().getHumidityScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_active_critical);
        }
        else{
            this.getViewPuppeteer().getmActivity().getHumidityScreen().getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
        }
    }

    /**
     * @brief Display a warning for the humidity
     */
    public void displayWarningHum(){
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getHumidityScreen()){
            this.getViewPuppeteer().getmActivity().getHumidityScreen().getWarning().setVisibility(View.VISIBLE);
        }
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getHomeScreen()){
            this.getViewPuppeteer().getmActivity().getHomeScreen().getWarning_hum().setVisibility(View.VISIBLE);
        }
    }

    /**
     * @brief Display a warning for the humidity
     */
    public void deleteWarningHum(){
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getHumidityScreen()){
            this.getViewPuppeteer().getmActivity().getHumidityScreen().getWarning().setVisibility(View.GONE);
        }
        if(this.getViewPuppeteer().getmActivity().getCurrentScreen() == this.getViewPuppeteer().getmActivity().getHomeScreen()){
            this.getViewPuppeteer().getmActivity().getHomeScreen().getWarning_hum().setVisibility(View.GONE);
        }
    }

    /**
     * @brief Change the humidity tab
     */
    public void changeConcerningHum(){
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                int thresholdHum;
                thresholdHum = getValueThresholdHum();
                if (thresholdHum == NORMAL) {
                    setTimerHumActivated(false);
                    changeTabHumNormal();
                    deleteWarningHum();
                    myTimer.removeCallbacksAndMessages(null);
                }
                if (thresholdHum == MODERATE) {
                    setTimerHumActivated(false);
                    changeTabHumModerate();
                    displayWarningHum();
                    myTimer.removeCallbacksAndMessages(null);
                }
                if (thresholdHum == CRITICAL) {
                    setTimerHumActivated(true);
                    changeTabHumCritical();
                    displayWarningHum();
                    displayPopUp();
                }
            }
        });
    }

    /**
     * @brief Reception of a new threshold
     */
    public void update(Observable obs, Object obj){
        int valueThreshold = Integer.parseInt(obj.toString());
        String name = obs.getClass().getSimpleName();
        if(name.equalsIgnoreCase("TemperatureThreshold")){
            setValueThresholdTemp(valueThreshold);
            this.viewPuppeteer.getmActivity().getBabyStateScreen().setValueThresholdTemp(valueThreshold);
            this.viewPuppeteer.getmActivity().getBrightnessScreen().setValueThresholdTemp(valueThreshold);
            this.viewPuppeteer.getmActivity().getHelpScreen().setValueThresholdTemp(valueThreshold);
            this.viewPuppeteer.getmActivity().getHomeScreen().setValueThresholdTemp(valueThreshold);
            this.viewPuppeteer.getmActivity().getHumidityScreen().setValueThresholdTemp(valueThreshold);
            this.viewPuppeteer.getmActivity().getSettingsScreen().setValueThresholdTemp(valueThreshold);
            this.viewPuppeteer.getmActivity().getTempScreen().setValueThresholdTemp(valueThreshold);
            changeConcerningTemp();
        }
        if(name.equalsIgnoreCase("HumidityThreshold")){
            setValueThresholdHum(valueThreshold);
            this.viewPuppeteer.getmActivity().getBabyStateScreen().setValueThresholdHum(valueThreshold);
            this.viewPuppeteer.getmActivity().getBrightnessScreen().setValueThresholdHum(valueThreshold);
            this.viewPuppeteer.getmActivity().getHelpScreen().setValueThresholdHum(valueThreshold);
            this.viewPuppeteer.getmActivity().getHomeScreen().setValueThresholdHum(valueThreshold);
            this.viewPuppeteer.getmActivity().getHumidityScreen().setValueThresholdHum(valueThreshold);
            this.viewPuppeteer.getmActivity().getSettingsScreen().setValueThresholdHum(valueThreshold);
            this.viewPuppeteer.getmActivity().getTempScreen().setValueThresholdHum(valueThreshold);
            changeConcerningHum();
        }
    }

    /*********** ACCESSORS ***********/
    public int getValueThresholdTemp() {
        return valueThresholdTemp;
    }

    public void setValueThresholdTemp(int valueThresholdTemp) {
        this.valueThresholdTemp = valueThresholdTemp;
    }

    public int getValueThresholdHum() {
        return valueThresholdHum;
    }

    public void setValueThresholdHum(int valueThresholdHum) {
        this.valueThresholdHum = valueThresholdHum;
    }

    public ViewPuppeteer getViewPuppeteer() {
        return viewPuppeteer;
    }

    public boolean isTimerTempActivated() {
        return timerTempActivated;
    }

    public void setTimerTempActivated(boolean timerTempActivated) {
        this.timerTempActivated = timerTempActivated;
    }

    public boolean isTimerHumActivated() {
        return timerHumActivated;
    }

    public void setTimerHumActivated(boolean timerHumActivated) {
        this.timerHumActivated = timerHumActivated;
    }
}
